/* $RCSFile: RecyclerViewPage.aspx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/Recycler/RecyclerViewPage.aspx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:22  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:59  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:26  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;

using AddonNice.Diagnostics;
using AddonNice.Configuration;
using AddonNice.Settings;
using AddonNice.Core;
using AddonNice.Helpers;
using AddonNice.UI;
using AddonNice.UI.WebControls;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;
                    
namespace AddonNice.PageModules.Recycler
{
    /// <summary>
    /// Module print page
    /// </summary>
    public partial class RecyclerViewPage : StdAuxPage
    {
		private int _moduleID;

        /// <summary>
        /// We only want a cancel button, other buttons will be added manually in OnPreInit and OnInit
        /// </summary>
        public RecyclerViewPage()
            : base(false, true, false,true)
        { }

		private void Page_Load(object sender, EventArgs e)
		{
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]RecyclerViewPage Page_Load BEG IsPostBack: {0}", IsPostBack),PageTraceSwitch.Sw.Info);
            // Ajaxyfy the restore Page Choice
		    AddAjaxSetting(ddTabs,customButton);
		    AddAjaxSetting(ddTabs,customButton2);
            
			try
			{
				_moduleID								=	ModuleID;
                if (PageTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]RecyClerViewPage Page_Load _moduleID: {0}", _moduleID),PageTraceSwitch.Sw.Info);
                if (PageTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]RecyclerViewPage Page_Load ModuleID: {0}", _moduleID),PageTraceSwitch.Sw.Info);
				ModuleConfiguration module			    =	new ModuleConfiguration(_moduleID);
                if (PageTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]RecyClerViewPage Page_Load module.PageID: {0}", module.PageID),PageTraceSwitch.Sw.Info);
				if ( module.PageID == StStr.PageIDNotAssigned )
				{
					if (!IsPostBack)
					{
						//load tab names for the dropdown list, then bind them
						ddTabs.DataBind();

                        //on initial load, disable the restore button until they make a selection
						customButton.Enabled            =   false;
						customButton2.Enabled           =   false;
					}			
					
					// create an instance of the module
                    string strCtl                       =   Path.ApplicationRoot + "/" + module.DesktopSrc;
                    PortalModuleControl myPortalModule  =   null;
                    try
                    {
                        if (PageTraceSwitch.Sw.TraceVerbose)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]RecyClerViewPage Page_Load LoadControl: {0}", strCtl),PageTraceSwitch.Sw.Info);
                        myPortalModule                      =   LoadPortalModule(strCtl, portalSettings.PortalID, module,true);
						PrintPlaceHolder.Controls.Add(myPortalModule);
                    }
                    catch (Exception ex)
                    {
                        if (PageTraceSwitch.Sw.TraceError)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]RecyclerViewPage Page_Load LoadControl: {0}, ex:{1}", strCtl,ex),PageTraceSwitch.Sw.Info);
                    }
				}			
				else  //they're trying to view a module that isn't in the recycler - maybe a manual manipulation of the url...?
				{
                    Label   lbl         =   new Label();
                    lbl.CssClass        =   "Error";
                    lbl.Text            =   LocalizeHelper.Localize("RECYCLER_MODULE_NOTFOUND","Module not found in recycler");
                    PrintPlaceHolder.Controls.Add(lbl);
                    if (PageTraceSwitch.Sw.TraceError)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]RecyclerViewPage Page_Load No Module: {0}.",_moduleID),PageTraceSwitch.Sw.Info);
				}
                if (PageTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine("[V]RecyclerViewPage Page_Load END.",PageTraceSwitch.Sw.Info);
			}
			catch (Exception ex)
			{
                if (PageTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]RecyclerViewPage Page_Load ex: {0}", ex),PageTraceSwitch.Sw.Info);
				throw;
			}
			
		}

        void RecyclerViewPage_Delete(object sender, AddonNice.UI.ADNPageEventArgs e)
		{
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]RecyClerViewPage RecyclerViewPage_Delete BEG.",PageTraceSwitch.Sw.Info);
            
			new ModulesDB().DeleteModule(_moduleID,Guid.Empty);
			_moduleID = 0;

            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]RecyClerViewPage RecyclerViewPage_Delete END.",PageTraceSwitch.Sw.Info);
		}

		#region Web Form Designer generated code
        /// <summary>
        /// Here we create the command buttons and add them to the PlaceHolders prepared
        /// </summary>
        /// <param name="e"></param>
        override protected void OnPreInit(EventArgs e)
        {
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]RecyclerViewPage OnPreInit BEG IsPostBack: {0}", IsPostBack),PageTraceSwitch.Sw.Info);
            //Controls must be created here
            HasDeleteExtender               =   true;
            CustomBtnName                   =   "Restore";
            HasCustomExtender               =   true;
            customButton                    =   NewImageButton("RestoreBtn");
            customButton2                   =   NewImageButton("RestoreBtn2");

            base.OnPreInit(e);
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]RecyclerViewPage OnPreInit END.",PageTraceSwitch.Sw.Info);
        }

        override protected void OnInit(EventArgs e)
		{
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]RecyClerViewPage OnInit BEG.",PageTraceSwitch.Sw.Info);
			InitializeComponent();
			base.OnInit(e);
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]RecyClerViewPage OnInit END.",PageTraceSwitch.Sw.Info);
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() 
		{    
			ddTabs.SelectedIndexChanged +=  new RadComboBoxSelectedIndexChangedEventHandler(ddTabs_SelectedIndexChanged);
            ddTabs.AutoPostBack         =   true;
			Load                        +=  new EventHandler(Page_Load);
            Delete                      +=  new PageEventHandler(RecyclerViewPage_Delete);
            CustomAction                +=  new PageEventHandler(RecyclerViewPage_CustomAction);
            //placeholders are not available in OnPreInit, we must add buttons to theme here

            PlaceHolderButtons.Controls.Add(customButton);
            PlaceHolderButtons2.Controls.Add(customButton2);

            PlaceHolderButtons.Controls.Add(new LiteralControl("&#160;"));
            PlaceHolderButtons2.Controls.Add(new LiteralControl("&#160;"));
		}


        protected override PlaceHolder placeHolderForButtons
        {
            get { return PlaceHolderButtons; }
            set { }
        }

        protected override PlaceHolder placeHolderForButtons2
        {
            get { return PlaceHolderButtons2; }
            set { }
        }

#endregion

        /// <summary>
        /// New Page is selected in combo
        /// </summary>
        void ddTabs_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]RecyClerViewPage ddTabs_SelectedIndexChanged BEG Value: [{0}]", e.Value.ToString()),PageTraceSwitch.Sw.Info);
            bool enableBtn          =   !(string.IsNullOrEmpty(e.Value));
            customButton.Enabled    = enableBtn;
            customButton2.Enabled   = enableBtn;
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]RecyClerViewPage ddTabs_SelectedIndexChanged END.",PageTraceSwitch.Sw.Info);
		}

        /// <summary>
        /// Restore button has been pressed
        /// </summary>
        void RecyclerViewPage_CustomAction(object sender, AddonNice.UI.ADNPageEventArgs e)
        {
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]RecyClerViewPage RecyclerViewPage_CustomAction BEG ddTabs.SelectedValue: [{0}]", ddTabs.SelectedValue.ToString()),PageTraceSwitch.Sw.Info);
            if ( string.IsNullOrEmpty(ddTabs.SelectedValue) )
                return;
			new RecyclerDB().MoveModuleToNewTab(int.Parse(ddTabs.SelectedValue),_moduleID);
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]RecyClerViewPage RecyclerViewPage_CustomAction END.",PageTraceSwitch.Sw.Info);
        }
   }
}